{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Make sure you have the latest version of agency-swarm installed\n",
    "You can uninstall the old version with `pip uninstall agency-swarm` and install the latest version with `pip install agency-swarm`"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "2c4b19dbe5ae6302"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from agency_swarm import Agent, Agency\n",
    "\n",
    "ceo = Agent(name=\"CEO\",\n",
    "            description=\"Responsible for client communication, task planning and management.\",\n",
    "            instructions=\"You must converse with other agents to ensure complete task execution.\", # can be a file like ./instructions.md\n",
    "            tools=[])\n",
    "\n",
    "test = Agent(name=\"Test Agent\",\n",
    "            description=\"Test agent\",\n",
    "            instructions=\"Please always respond with 'test complete'\", # can be a file like ./instructions.md\n",
    "            tools=[])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-29T03:34:23.557863Z",
     "start_time": "2024-01-29T03:34:23.157979Z"
    }
   },
   "id": "a16ee4220f5ab03a",
   "execution_count": 1
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Loading agents and threads from DB example"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8d99382d99b7a8ac"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# threads is an object\n",
    "threads = {}\n",
    "\n",
    "def load_threads():\n",
    "    # your code to load threads from DB here\n",
    "    # we simply use a global variable for this example\n",
    "    global threads\n",
    "    return threads\n",
    "\n",
    "def save_threads(new_threads):\n",
    "    # your code to save new_threads to DB here\n",
    "    global threads\n",
    "    threads = new_threads"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ba3f9da173f7a0b6"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# settings is an array of objects with your agent settings\n",
    "settings = []\n",
    "\n",
    "def load_settings():\n",
    "    # your code to load settings from DB here\n",
    "    # we simply use a global variable for this example\n",
    "    global settings\n",
    "    return settings\n",
    "\n",
    "def save_settings(new_settings):\n",
    "    # your code to save new_settings to DB here\n",
    "    global settings\n",
    "    settings = new_settings"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "7920db25caf29803"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Creating agency with loaded agents and threads"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "88734e5628b23466"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "agency = Agency([ceo, [ceo, test]], \n",
    "                async_mode='threading', # only threading is supported for now\n",
    "                threads_callbacks={'load': load_threads, 'save': save_threads},\n",
    "                settings_callbacks={'load': load_settings, 'save': save_settings})"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a65a5acb4bc6bbc3"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "THREAD:[ user -> CEO ]: URL https://platform.openai.com/playground?assistant=asst_HQ3kpb9SzhEgo0ya4IvSFoQ8&mode=assistant&thread=thread_cn2VhmbuYcr0EZgcGVIEOSfw\n",
      "THREAD:[ CEO -> Test Agent ]: URL https://platform.openai.com/playground?assistant=asst_cml8LF575HVYy7cWePbEQDgy&mode=assistant&thread=thread_YEsfiNS8gOyGOXXMLoZEBJZz\n"
     ]
    },
    {
     "data": {
      "text/plain": "\"I've sent a greeting to the Test Agent. You can ask me to check for a response later, and I'll be happy to do so!\""
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agency.get_completion(\"Say hi to test agent\", yield_messages=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-23T04:17:09.976706Z",
     "start_time": "2024-01-23T04:17:01.254030Z"
    }
   },
   "id": "f578f37d8b261559",
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "THREAD:[ user -> CEO ]: URL https://platform.openai.com/playground?assistant=asst_HQ3kpb9SzhEgo0ya4IvSFoQ8&mode=assistant&thread=thread_cn2VhmbuYcr0EZgcGVIEOSfw\n"
     ]
    },
    {
     "data": {
      "text/plain": "'The Test Agent has completed the task. If you have any more requests or tasks, feel free to let me know!'"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agency.get_completion(\"Check status\", yield_messages=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-23T04:17:37.434522Z",
     "start_time": "2024-01-23T04:17:30.450008Z"
    }
   },
   "id": "fc798bc6c58c9c16",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7861\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/plain": "<IPython.core.display.HTML object>",
      "text/html": "<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "Gradio Blocks instance: 2 backend functions\n-------------------------------------------\nfn_index=0\n inputs:\n |-textbox\n |-chatbot\n outputs:\n |-textbox\n |-chatbot\nfn_index=1\n inputs:\n |-chatbot\n outputs:\n |-chatbot"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agency.demo_gradio()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-21T03:36:13.946078Z",
     "start_time": "2024-01-21T03:36:13.843445Z"
    }
   },
   "id": "6ef9050ecc718655",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "94e0b7064cfb2c62"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
